home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 18.8 KB | 671 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWBmpShp.cpp
- // Release Version: $ 1.0d1 $
- //
- // Creation Date: 4/11/94
- //
- // Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWBMPSHP_H
- #include "FWBmpShp.h"
- #endif
-
- #ifndef FWSHAPE_H
- #include "FWShape.h"
- #endif
-
- #ifndef FWDFAULT_H
- #include "FWDfault.h"
- #endif
-
- #ifndef FWGC_H
- #include "FWGC.h"
- #endif
-
- #ifndef FWPICSHP_H
- #include "FWPicShp.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _TRNSFORM_
- #include <Trnsform.h>
- #endif
-
- // ----- Macintosh Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(mathRoutinesIncludes)
- #include <math routines.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__QUICKDRAW__)
- #include <QuickDraw.h>
- #endif
-
- //========================================================================================
- // •• RunTime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment fwgraphx
- #endif
-
- //========================================================================================
- // •• CLASS FW_CBitmapShape
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShape::FW_CBitmapShape
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapShape::FW_CBitmapShape() :
- FW_CShape()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShape::FW_CBitmapShape
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapShape::FW_CBitmapShape(char* image, short width, short height, short rowbytes, short pixelSize, const FW_CRect& rect) :
- FW_CShape()
- {
- FW_CRect dstRect(rect);
- if (dstRect == FW_kZeroRect)
- dstRect.Set(0, 0, ff(width), ff(height));
- SetRep(new FW_CBitmapShapeRep(image, width, height, rowbytes, pixelSize, dstRect));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShape::FW_CBitmapShape
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapShape::FW_CBitmapShape(short width, short height, short pixelSize, const FW_CRect& rect) :
- FW_CShape()
- {
- FW_CRect dstRect(rect);
- if (dstRect == FW_kZeroRect)
- dstRect.Set(0, 0, ff(width), ff(height));
- SetRep(new FW_CBitmapShapeRep(NULL, width, height, 0, pixelSize, dstRect));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShape::FW_CBitmapShape
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapShape::FW_CBitmapShape(FW_PlatformPict platformPict, const FW_CRect& rect) :
- FW_CShape()
- {
- FW_CRect dstRect(rect);
- if (dstRect == FW_kZeroRect)
- {
- #ifdef FW_BUILD_MAC
- dstRect = (*platformPict)->picFrame;
- dstRect.Place(0,0);
- #endif
- }
- SetRep(new FW_CBitmapShapeRep(platformPict, rect));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShape::FW_CBitmapShape
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapShape::FW_CBitmapShape(const FW_CBitmapShape& other) :
- FW_CShape() // The compiler refuse to take FW_CShape(other)
- {
- SetRep(other.GetRep());
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShape::FW_CBitmapShape
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapShape::FW_CBitmapShape(FW_CBitmapShapeRep* rep) :
- FW_CShape(rep)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShape::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapShape& FW_CBitmapShape::operator=(const FW_CBitmapShape& other)
- {
- SetRep(other.GetRep());
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShape::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapShape& FW_CBitmapShape::operator=(FW_CBitmapShapeRep* other)
- {
- SetRep(other);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShape::operator FW_CPictShape
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapShape::operator FW_CPictShape() const
- {
- FW_CPictShape pictShape((FW_PlatformPict)NULL, FW_kZeroRect);
- ((FW_CBitmapShapeRep*)GetRep())->ConvertToPict(pictShape);
- return pictShape;
- }
-
- //========================================================================================
- // •• CLASS FW_CBitmapShapeRep
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShapeRep::FW_CBitmapShapeRep
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapShapeRep::FW_CBitmapShapeRep() :
- FW_CBoundedShapeRep(gGraphicGlobales.gBitmap, FW_kZeroRect),
- #ifdef FW_BUILD_MAC
- fMacGWorld(NULL),
- fInitialPixelSize(0)
- #endif
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShapeRep::FW_CBitmapShapeRep
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapShapeRep::FW_CBitmapShapeRep(char* image,
- short width, short height,
- short rowbytes, short pixelSize,
- const FW_CRect& rect) :
- FW_CBoundedShapeRep(gGraphicGlobales.gBitmap, rect),
- #ifdef FW_BUILD_MAC
- fMacGWorld(NULL),
- fInitialPixelSize(pixelSize)
- #endif
- {
-
- #ifdef FW_BUILD_MAC
- NewGWorld(width, height, pixelSize);
- MacSetImage(image, rowbytes);
- #endif
-
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShapeRep::FW_CBitmapShapeRep
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapShapeRep::FW_CBitmapShapeRep(FW_PlatformPict platformPict, const FW_CRect& rect) :
- FW_CBoundedShapeRep(gGraphicGlobales.gBitmap, rect)
- {
- #ifdef FW_BUILD_MAC
- Rect pictRect = rect;
- ::OffsetRect(&pictRect, -pictRect.left, -pictRect.top);
-
- fInitialPixelSize = 0;
- NewGWorld(pictRect.right, pictRect.bottom, 0);
- MacSetImage(platformPict, pictRect);
- #endif
-
- #ifdef FW_BUILD_WIN
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShapeRep::~FW_CBitmapShapeRep
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapShapeRep::~FW_CBitmapShapeRep()
- {
- #ifdef FW_BUILD_MAC
- if (fMacGWorld)
- DisposeGWorld(fMacGWorld);
- fMacGWorld = NULL;
- #endif
-
- #ifdef FW_BUILD_WIN
- #endif
- }
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShapeRep::NewGWorld
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::NewGWorld(short width, short height, short pixelSize)
- {
- GWorldPtr world = NULL;
-
- fHeight = height;
- fWidth = width;
- if (fHeight != 0 && fWidth != 0)
- {
- Rect boundsRect = {0, 0, fHeight, fWidth};
-
- if (::NewGWorld(&world, pixelSize, &boundsRect, NULL, NULL, 0) != noErr)
- {
- // change to exception
- FW_DEBUG_MESSAGE("NewGWorld error");
- }
- }
-
- fMacGWorld = world;
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShapeRep::MacSetImage
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::MacSetImage(char* image, short rowBytes)
- {
- if (image != NULL)
- {
- PixMapHandle pixMap = LockPixels();
-
- Ptr baseAddr = ::GetPixBaseAddr(pixMap);
-
- if ((*pixMap)->rowBytes == rowBytes)
- {
- // ----- Copy in one block -----
- FW_PrimitiveCopyMemory(image, baseAddr, fHeight * rowBytes);
- }
- else
- {
- // ----- Copy line by line -----
- Ptr source = image;
- short srcRowBytes = rowBytes;
- Ptr destination = baseAddr;
- short dstRowBytes = (*pixMap)->rowBytes;
- short nbBytes = srcRowBytes < dstRowBytes ? srcRowBytes : dstRowBytes;
- for (short i = 0; i<fHeight; i++)
- {
- FW_PrimitiveCopyMemory(source, destination, nbBytes);
- source += srcRowBytes;
- destination += dstRowBytes;
- }
- }
-
- ::UnlockPixels(pixMap);
- }
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShapeRep::LockPixels
- //----------------------------------------------------------------------------------------
-
- PixMapHandle FW_CBitmapShapeRep::LockPixels()
- {
- PixMapHandle pixMap = ::GetGWorldPixMap(fMacGWorld);
- ::LockPixels(pixMap);
- return pixMap;
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShapeRep::MacSetImage
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::MacSetImage(PicHandle picture, const FW_SPlatformRect& dstRect)
- {
- CGrafPtr curPort;
- GDHandle gdh;
- ::GetGWorld(&curPort, &gdh);
- ::SetGWorld(fMacGWorld, NULL);
-
- PixMapHandle pmh = LockPixels();
- ::DrawPicture(picture, &dstRect);
- ::UnlockPixels(pmh);
-
- ::SetGWorld(curPort, gdh);
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShapeRep::HitTest
- //----------------------------------------------------------------------------------------
-
- FW_HitTestPart FW_CBitmapShapeRep::HitTest(const FW_CPoint& test) const
- {
- FW_DEBUG_MESSAGE("Not Yet Implemented");
- return FW_kOutside;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapShapeRep::Draw
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::Draw(FW_CGraphicContext* gc)
- {
- FW_ShapeFills shapeFill = GetShapeFill();
-
- if (shapeFill == FW_kNullShape)
- return;
-
- gc->SelectInkAndStyle(GetShapeInk(), GetShapeStyle(), FW_kBitmapShape, shapeFill);
-
- FW_SPlatformRect dstRect = gc->AsPlatformRect(fRect);
-
- #ifdef FW_BUILD_MAC
- Rect srcRect = {0, 0, fHeight, fWidth};
- PixMapHandle pmh = LockPixels();
- ::CopyBits((BitMap*)*pmh, &XMPASLMQDGlobals.thePort->portBits,
- &srcRect, &dstRect,
- GetShapeInk()->GetTransferMode(), NULL);
- ::UnlockPixels(pmh);
- #endif
-
- #ifdef FW_BUILD_WIN
- #endif
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::SetAsDefault
- //------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::SetAsDefault() const
- {
- SetDefaultProperties(gGraphicGlobales.gBitmap);
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::GetPixel
- //------------------------------------------------------------------------------
-
- long FW_CBitmapShapeRep::GetPixel(short x, short y) const
- {
- FW_DEBUG_MESSAGE("Not Yet Implemented");
- return 0;
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::GetPixel
- //------------------------------------------------------------------------------
-
- long FW_CBitmapShapeRep::GetPixel(short x, short y, FW_CColor& color) const
- {
- FW_DEBUG_MESSAGE("Not Yet Implemented");
- return 0;
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::SetPixel
- //------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::SetPixel(short x, short y, long index)
- {
- FW_DEBUG_MESSAGE("Not Yet Implemented");
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::SetPixel
- //------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::SetPixel(short x, short y, const FW_CColor& color)
- {
- FW_DEBUG_MESSAGE("Not Yet Implemented");
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::GetBitmapInfo
- //------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::GetBitmapInfo(short& width,
- short& height,
- short& rowBytes,
- short& pixelSize) const
- {
- width = fWidth;
- height = fHeight;
-
- #ifdef FW_BUILD_MAC
- PixMapHandle pixMap = ::GetGWorldPixMap(fMacGWorld);
- rowBytes = (*pixMap)->rowBytes ^ 0x8000;
- pixelSize = (*pixMap)->pixelSize;
- #endif
-
- #ifdef FW_BUILD_WIN
- FW_DEBUG_MESSAGE("Not Yet Implemented");
- #endif
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::ChangeBitmap
- //------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::ChangeBitmap(char* image, short width, short height, short rowBytes, short pixelSize)
- {
- #ifdef FW_BUILD_MAC
- if (fMacGWorld == NULL)
- {
- NewGWorld(width, height, pixelSize);
- }
- else
- {
- Rect boundsRect = {0, 0, height, width};
- ::UpdateGWorld(&fMacGWorld, pixelSize, &boundsRect, NULL, NULL, 0);
- }
- MacSetImage(image, rowBytes);
- fInitialPixelSize = pixelSize;
- #endif
-
- Changed();
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::SetBitmapParts
- //------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::SetBitmapParts(FW_PlatformPict platformPict, const FW_SPlatformRect& bounds)
- {
- #ifdef FW_BUILD_MAC
- if (fMacGWorld == NULL)
- NewGWorld(bounds.right - bounds.left, bounds.bottom - bounds.top, 0);
-
- MacSetImage(platformPict, bounds);
- fInitialPixelSize = 0;
- #endif
-
- #ifdef FW_BUILD_WIN
- #endif
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::GetBitmapParts
- //------------------------------------------------------------------------------
-
- FW_CBitmapShape FW_CBitmapShapeRep::GetBitmapParts(const FW_CRect& bounds)
- {
- // ATTENTION: temporary code
- #ifdef FW_BUILD_MAC
- Rect srcRect = bounds;
- if (srcRect.right > fWidth)
- srcRect.right = fWidth;
- if (srcRect.bottom > fHeight)
- srcRect.bottom = fHeight;
-
- Rect dstRect = srcRect;
- ::OffsetRect(&dstRect, -dstRect.left, -dstRect.top);
-
- short width, height, pixelSize, rowBytes;
- GetBitmapInfo(width, height, rowBytes, pixelSize);
-
- FW_CBitmapShape dstBmpShape(dstRect.right, dstRect.bottom, pixelSize);
-
- CopyPixels(dstBmpShape, srcRect, dstRect);
-
- return dstBmpShape;
- #endif
-
- #ifdef FW_BUILD_WIN
- #endif
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::CopyPixels
- //------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::CopyPixels(const FW_CBitmapShape& dstShape,
- const FW_SPlatformRect& srcRect,
- const FW_SPlatformRect& dstRect)
- {
- #ifdef FW_BUILD_MAC
- PixMapHandle srcPixMap = LockPixels();
- PixMapHandle dstPixMap = dstShape->LockPixels();
-
- CGrafPtr curPort;
- GDHandle gdh;
- ::GetGWorld(&curPort, &gdh);
- ::SetGWorld(fMacGWorld, NULL);
- ::CopyBits((BitMap*)*srcPixMap, (BitMap*)*dstPixMap,
- &srcRect, &dstRect,
- srcCopy, NULL);
- ::SetGWorld(curPort, gdh);
-
- ::UnlockPixels(dstPixMap);
- ::UnlockPixels(srcPixMap);
- #endif
-
- #ifdef FW_BUILD_WIN
- #endif
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::SetBitmapParts
- //------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::SetBitmapParts(const FW_CBitmapShape& bitmapShape, const FW_SPlatformRect& bounds)
- {
- // ATTENTION: temporary code
- #ifdef FW_BUILD_MAC
- Rect dstRect = bounds;
- if (dstRect.right > fWidth)
- dstRect.right = fWidth;
- if (dstRect.bottom > fHeight)
- dstRect.bottom = fHeight;
-
- short width, height, pixelSize, rowBytes;
- GetBitmapInfo(width, height, rowBytes, pixelSize);
-
- Rect srcRect = {0, 0, dstRect.bottom - dstRect.top, dstRect.right - dstRect.left};
- if (srcRect.right > width)
- srcRect.right = width;
- if (srcRect.bottom > height)
- srcRect.bottom = height;
-
- dstRect = srcRect;
- ::OffsetRect(&dstRect, bounds.left, bounds.top);
-
- FW_CBitmapShape dstShape(this);
- bitmapShape->CopyPixels(dstShape, srcRect, dstRect);
- #endif
-
- #ifdef FW_BUILD_MAC
- #endif
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::Flatten
- //------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::Flatten(FW_CWritableStream& stream)
- {
- FW_CBoundedShapeRep::Flatten(stream);
-
- #ifdef FW_BUILD_MAC
- // ATTENTION: temporary code
- stream << fHeight;
- stream << fWidth;
-
- PixMapHandle pixMap = LockPixels();
- stream << (*pixMap)->pixelSize;
-
- short rowBytes = (*pixMap)->rowBytes ^ 0x8000;
- long bitmapSize = ((long)fHeight) * rowBytes;
- stream.Write((*pixMap)->baseAddr, bitmapSize);
-
- ::UnlockPixels(pixMap);
- #endif
-
- #ifdef FW_BUILD_WIN
- #endif
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::Unflatten
- //------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::Unflatten(FW_CReadableStream& stream)
- {
- FW_CBoundedShapeRep::Unflatten(stream);
-
- #ifdef FW_BUILD_MAC
- // ATTENTION: temporary code
- if (fMacGWorld)
- DisposeGWorld(fMacGWorld);
- fMacGWorld = NULL;
-
- stream >> fHeight;
- stream >> fWidth;
- stream >> fInitialPixelSize;
-
- NewGWorld(fWidth, fHeight, fInitialPixelSize);
-
- PixMapHandle pixMap = LockPixels();
-
- short rowBytes = (*pixMap)->rowBytes ^ 0x8000;
- long bitmapSize = ((long)fHeight) * rowBytes;
- stream.Read((*pixMap)->baseAddr, bitmapSize);
-
- ::UnlockPixels(pixMap);
- #endif
-
- #ifdef FW_BUILD_WIN
- #endif
- }
-
- //------------------------------------------------------------------------------
- // • FW_CBitmapShapeRep::ConvertToPict
- //------------------------------------------------------------------------------
-
- void FW_CBitmapShapeRep::ConvertToPict(FW_CPictShape& pictShape)
- {
- #ifdef FW_BUILD_MAC
- // ATTENTION: temporary code
- CGrafPtr curPort;
- GDHandle gdh;
- ::GetGWorld(&curPort, &gdh);
-
- PixMapHandle pixMap = LockPixels();
-
- ::SetGWorld(fMacGWorld, NULL);
-
- Rect picRect = {0, 0, fHeight, fWidth};
- PicHandle hPict = ::OpenPicture(&picRect);
- ::CopyBits((BitMap*)*pixMap, (BitMap*)*pixMap,
- &picRect, &picRect,
- srcCopy, NULL);
- ::ClosePicture();
-
- pictShape->SetRectangle(fRect);
- pictShape->SetPlatformPict(hPict);
-
- SetGWorld(curPort, gdh);
-
- ::UnlockPixels(pixMap);
- #endif
- #ifdef FW_BUILD_WIN
- #endif
- }